////////////////////////////////////////////////////////////////////////////////
//////// REGIME DATABASE 
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
//////// a. Building a list of regimes
////////////////////////////////////////////////////////////////////////////////

// We extract from V-Dem a list of regimes with a name, start and end date. For county*years not documented by V-Dem, we manually divide periods into regimes and code for each one a name, start date and end date.

use "$project_path/data/1_input/elections/V-Dem/vdem_cd_extract.dta", clear

keep country_name year historical_date v2reginfo
ren v2reginfo regime
ren country_name Country
ren year Year

format Country %25s
format regime %35s

// V-Dem regimes with typos
replace regime = "Cambodia under the CPP (06/07/1997 - 16/11/2017)" if regime=="Cambodia under the CPP (06/07/1997 - 17/17/2017)"
replace regime = "De facto independent Somaliland (18/05/1991 - 13/04/2003)" if regime=="De facto independent Somaliland (18/05/1991 - 04/14/2003)"
replace regime = "Post- 2008 constitutional rule (29/09/2008 - E)" if (regime=="Post- 1979 constitutional rule (11/08/1979 - E)" | regime=="Post- 2008 constitutional rule (11/08/1979 - E)") & historical_date>date("29/09/2008", "DMY")
replace regime = "1979-2008 constitutional rule (11/08/1979 - 28/09/2008)" if (regime=="Post- 1979 constitutional rule (11/08/1979 - E)" | regime=="Post- 2008 constitutional rule (11/08/1979 - E)") & historical_date<date("29/09/2008", "DMY")

// Adjusting country names
replace Country="Bosnia-Herz"  if Country=="Bosnia and Herzegovina"
replace Country="Myanmar"  if Country=="Burma/Myanmar"
replace Country="C. Verde Is."  if Country=="Cape Verde"
replace Country="Cent. Af. Rep."  if Country=="Central African Republic"
replace Country="PRC"  if Country=="China"
replace Country="Comoro Is."  if Country=="Comoros"
replace Country="Czech Rep."  if Country=="Czech Republic"
replace Country="Congo (DRC)"  if Country=="Democratic Republic of the Congo"
replace Country="Dom. Rep."  if Country=="Dominican Republic"
replace Country="Eq. Guinea"  if Country=="Equatorial Guinea"
replace Country="Swaziland"  if Country=="Eswatini"
replace Country="GDR"  if Country=="German Democratic Republic"
replace Country="FRG/Germany"  if Country=="Germany"
replace Country="Macedonia"  if Country=="North Macedonia"
replace Country="Micronesia, Federated States of"  if Country=="Micronesia"
replace Country="PRK"  if Country=="North Korea"
replace Country="P. N. Guinea"  if Country=="Papua New Guinea"
replace Country="Congo"  if Country=="Republic of the Congo"
replace Country="Solomon Is."  if Country=="Solomon Islands"
replace Country="S. Africa"  if Country=="South Africa"
replace Country="Korea, Republic of"  if Country=="South Korea"
replace Country="Gambia"  if Country=="The Gambia"
replace Country="Trinidad-Tobago"  if Country=="Trinidad and Tobago"
replace Country="UAE"  if Country=="United Arab Emirates"
replace Country="UK"  if Country=="United Kingdom"
replace Country="USA"  if Country=="United States of America"
drop if Country=="Baden"
drop if Country=="Bavaria"
drop if Country=="Brunswick"
drop if Country=="Hamburg"
drop if Country=="Hanover"
drop if Country=="Hesse-Darmstadt"
drop if Country=="Hesse-Kassel"
drop if Country=="Hong Kong"
drop if Country=="Mecklenburg Schwerin"
drop if Country=="Modena"
drop if Country=="Nassau"
drop if Country=="Oldenburg"
drop if Country=="Papal States"
drop if Country=="Parma"
drop if Country=="Piedmont-Sardinia"
drop if Country=="Republic of Vietnam"
drop if Country=="Saxe-Weimar-Eisenach"
drop if Country=="Saxony"
drop if Country=="Tuscany"
drop if Country=="Two Sicilies"
drop if Country=="Würtemberg"
drop if Country=="Zanzibar"
drop if Country=="Palestine/Gaza"
drop if Country=="Palestine/British Mandate" & Year > 1947
drop if Country=="Palestine/West Bank" & Year <=1948
replace Country="Occupied Palestinian Territories"  if Country=="Palestine/British Mandate"
replace Country="Occupied Palestinian Territories"  if Country=="Palestine/West Bank"

// We extract start dates and end dates for each regime documented by V-Dem
keep Country regime
duplicates drop
drop if regime==""

gen regime_dates = substr(regime, strrpos(regime, "("), strlen(regime) - strpos(regime, "(") + 1)
replace regime_dates = substr(regime_dates, 1, strpos(regime_dates, ")"))
replace regime_dates = subinstr(regime_dates, " ", "", .)
replace regime_dates = subinstr(regime_dates, "(", "", .)
replace regime_dates = subinstr(regime_dates, ")", "", .)
split regime_dates, parse("-")
drop regime_dates
ren regime_dates1 start_date_
ren regime_dates2 end_date_
replace end_date_ = "31/12/2018" if end_date_=="E"
gen start_date = date(start_date_, "DMY")
gen end_date = date(end_date_, "DMY")
drop start_date_ end_date_
format start_date end_date %td
format end_date %td

// We drop regimes starting after Dec 31st 2018 or ending before Jan 1st 1946
drop if start_date>date("31Dec2018", "DMY")
drop if end_date<date("1Jan1946", "DMY")
sort Country start_date
tempfile vdem_regime_list
save `vdem_regime_list'

// Adding manually coded regimes
import excel "$project_path/data/1_input/other/regimes/manual_regimes.xlsx", sheet("Sheet1") firstrow clear
keep Country reg_start reg_end regime
tostring  reg_start reg_end, replace
replace reg_start="01/01/"+reg_start
replace reg_end="31/12/"+reg_end
gen start_date = date(reg_start,"DMY")
gen end_date = date(reg_end,"DMY")
format start_date %td
format end_date %td
drop reg_start reg_end
append using `vdem_regime_list'
sort Country start_date

// We check that all regimes have a start and end date
assert start_date!=. & end_date!=.

compress
format Country %25s
format regime %30s
save "$project_path/data/2_intermediary/regimes/regime_list", replace

// Start and end year of regimes
gen reg_start_year = year(start_date)
gen reg_end_year = year(end_date)
tempfile regime_start_end_years
save `regime_start_end_years'

////////////////////////////////////////////////////////////////////////////////
//////// b. Associating elections with regimes
////////////////////////////////////////////////////////////////////////////////

// Gathering election dates
use "$project_path/data/3_cleaned/presidential_elections.dta", clear
keep Country Year Month Type_Election Date
tempfile pres_elec_dates
save `pres_elec_dates'
use "$project_path/data/3_cleaned/parliamentary_elections.dta", clear
keep Country Year Month Type_Election Date
append using `pres_elec_dates'
sort Country Date
bysort Country : gen elec_no = _n
tempfile elec_dates
save `elec_dates'

// Number of elections 
local nb_elec = _N

// Associating all election dates with all regimes
collapse (max) elec_no, by(Country)
ren elec_no nb_elec
merge 1:m Country using "$project_path/data/2_intermediary/regimes/regime_list", assert(using match) keep(match) nogen
expand(nb_elec)
bysort Country regime: gen elec_no = _n
drop nb_elec
merge m:1 Country elec_no using `elec_dates', assert(match) nogen

// Only keep compatible regime*date pairs
drop if (Date>end_date | Date<start_date) & Date!=.
drop if Year>year(end_date) | Year<year(start_date)

// Special cases
drop if Country=="Bangladesh" & Year==2014 & regime!="2008-2014 constitutional alternation (07/01/2009 - 05/01/2014)"
drop if Country=="Ethiopia" & Year==1955 & regime!="Ethiopian-Eritrean Federation under Haile Selassie (16/09/1952 - 04/11/1955)"
drop if Country=="Montenegro" & Year==1992 & regime!="Montenegro part of Federal Republic of Yugoslavia (28/04/1992 - 04/02/2003)"
drop if Country=="Myanmar" & Year==2015 & regime!="Myanmar under (called SLORC until 1997) SPDC (19/09/1988 - 08/11/2015)"
drop if Country=="Ukraine" & Year==1991 & regime!="Independent Ukraine  (25/08/1991 - 23/01/2005)"
drop if Country=="Tajikistan" & Year==1991 & regime!="Independent Tajikistan under Nabiyev (10/09/1991 - 07/09/1992)"
drop if Country=="Kyrgyzstan" & Year==1991 & regime!="Independent Kyrgyzstan under Akayev (01/09/1991 - 24/03/2005)"
drop if Country=="Georgia" & Year==1991 & regime!="Republic of Georgia (10/04/1991 - 22/12/1991)"

// Check that each election is only associated with only one regime
duplicates tag Country Year Month Type_Election, gen(temp)
assert temp==0
drop temp

// Check that all elections have been allocated with a regime
assert regime!=""
merge 1:1 Country Year Month Type_Election using `elec_dates', assert(match) nogen
keep Country Year Month Type_Election regime
order Country Year Month Type_Election regime
sort Type_Election Country Year Month

compress
format Country %25s
format regime %30s
lab var regime "Regime name"

save "$project_path/data/2_intermediary/regimes/elections_regime_match", replace

////////////////////////////////////////////////////////////////////////////////
//////// c. Characterizing regimes in V-Dem
////////////////////////////////////////////////////////////////////////////////

// List of regimes with elections
use "$project_path/data/2_intermediary/regimes/elections_regime_match", clear
gen elec_pres = (Type_Election=="Presidential")
gen elec_parl = (Type_Election=="Parliamentary")
collapse (max) elec_pres elec_parl, by(Country regime)
lab var elec_pres "At least one presidential election held"
lab var elec_parl "At least one parliamentary election held"
format Country %25s
format regime %30s
tempfile regime_elec_types
save `regime_elec_types'

// Manually added regime types
import excel "$project_path/data/1_input/other/regimes/manual_regime_types.xlsx", sheet("Sheet1") firstrow clear
format Country %25s
format regime %30s
tempfile manual_regime_types
save `manual_regime_types'

// Manually added regimes
import excel "$project_path/data/1_input/other/regimes/manual_regimes.xlsx", sheet("Sheet1") firstrow clear
format Country %25s
format regime %30s
tempfile manual_regimes
save `manual_regimes'

// Importing V-Dem data
use "$project_path/data/1_input/elections/V-Dem/vdem_cy_extract.dta", clear
ren country_name Country
ren year Year
ren v2reginfo regime
drop if regime==""
order Country Year regime
format Country %25s
format regime %30s
replace regime = "Cambodia under the CPP (06/07/1997 - 16/11/2017)" if regime=="Cambodia under the CPP (06/07/1997 - 17/17/2017)"
replace regime = "De facto independent Somaliland (18/05/1991 - 13/04/2003)" if regime=="De facto independent Somaliland (18/05/1991 - 04/14/2003)"
replace regime = "Post- 2008 constitutional rule (29/09/2008 - E)" if (regime=="Post- 1979 constitutional rule (11/08/1979 - E)" | regime=="Post- 2008 constitutional rule (11/08/1979 - E)") & historical_date>date("29/09/2008", "DMY")
replace regime = "1979-2008 constitutional rule (11/08/1979 - 28/09/2008)" if (regime=="Post- 1979 constitutional rule (11/08/1979 - E)" | regime=="Post- 2008 constitutional rule (11/08/1979 - E)") & historical_date<date("29/09/2008", "DMY")
replace Country="Bosnia-Herz"  if Country=="Bosnia and Herzegovina"
replace Country="Myanmar"  if Country=="Burma/Myanmar"
replace Country="C. Verde Is."  if Country=="Cape Verde"
replace Country="Cent. Af. Rep."  if Country=="Central African Republic"
replace Country="Congo (DRC)"  if Country=="Democratic Republic of the Congo"
replace Country="PRC"  if Country=="China"
replace Country="Comoro Is."  if Country=="Comoros"
replace Country="Czech Rep."  if Country=="Czech Republic"
replace Country="Vietnam"  if Country=="Democratic Republic of Vietnam"
replace Country="Dom. Rep."  if Country=="Dominican Republic"
replace Country="Eq. Guinea"  if Country=="Equatorial Guinea"
replace Country="Swaziland"  if Country=="Eswatini"
replace Country="GDR"  if Country=="German Democratic Republic"
replace Country="FRG/Germany"  if Country=="Germany"
replace Country="PRK"  if Country=="North Korea"
replace Country="Macedonia"  if Country=="North Macedonia"
replace Country="P. N. Guinea"  if Country=="Papua New Guinea"
drop if Country=="Republic of Vietnam"
replace Country="Congo"  if Country=="Republic of the Congo"
replace Country="Solomon Is."  if Country=="Solomon Islands"
replace Country="S. Africa"  if Country=="South Africa"
replace Country="Korea, Republic of"  if Country=="South Korea"
replace Country="Sao Tome and Principe"  if Country=="São Tomé and Príncipe"
replace Country="Gambia"  if Country=="The Gambia"
replace Country="Trinidad-Tobago"  if Country=="Trinidad and Tobago"
replace Country="UAE"  if Country=="United Arab Emirates"
replace Country="UK"  if Country=="United Kingdom"
replace Country="USA"  if Country=="United States of America"
drop if Country=="Baden"
drop if Country=="Bavaria"
drop if Country=="Brunswick"
drop if Country=="Hamburg"
drop if Country=="Hanover"
drop if Country=="Hesse-Darmstadt"
drop if Country=="Hesse-Kassel"
drop if Country=="Hong Kong"
drop if Country=="Mecklenburg Schwerin"
drop if Country=="Modena"
drop if Country=="Nassau"
drop if Country=="Oldenburg"
drop if Country=="Papal States"
drop if Country=="Parma"
drop if Country=="Piedmont-Sardinia"
drop if Country=="Saxe-Weimar-Eisenach"
drop if Country=="Saxony"
drop if Country=="Tuscany"
drop if Country=="Two Sicilies"
drop if Country=="Würtemberg"
drop if Country=="Zanzibar"
drop if Country=="Palestine/Gaza"
drop if Country=="Palestine/British Mandate" & Year > 1947
drop if Country=="Palestine/West Bank" & Year <=1948
replace Country="Occupied Palestinian Territories"  if Country=="Palestine/British Mandate"
replace Country="Occupied Palestinian Territories"  if Country=="Palestine/West Bank"

// Sometimes the HOS and HOG are the same person, but V-Dem only mentions one. We fill in the blanks, using the V-Dem variable indicating whether the HOS and HOG are the same person.
replace v2exnamhog = v2exnamhos if v2exhoshog==1 & v2exnamhog==""
replace v2exnamhos = v2exnamhog if v2exhoshog==1 & v2exnamhos==""
replace v2expathhs = v2expathhg if v2exhoshog==1 & v2expathhs==.
replace v2expathhg = v2expathhs if v2exhoshog==1 & v2expathhg==. 
label define expathhg 0 "Coup/Rebellion" 1 "Foreign power" 2 "Ruling party" 3 "Royal council" 4 "Hereditary" 5 "Military" 6 "Appointed by HOS" 7 "Legislature" 8 "Popular election" 9 "Other"
label values v2expathhg expathhg 

// We use the five "power" variables defined by V-Dem to quantify the amount of power enjoyed by the HOS and HOG. We normalize each of these variables so that they are within the [0,1] interval.
*HOS forms of power
gen hos_disleg=v2exdfdshs_ord/3 if v2exdfdshs_ord!=.
lab var hos_disleg "HOS power to dissolve legislative assembly"
gen hos_appointcab=v2exdfcbhs_ord/4 if v2exdfcbhs_ord!=.
lab var hos_appointcab "HOS power to appoint cabinet members"
gen hos_discab=v2exdfdmhs_ord/3 if v2exdfdmhs_ord!=.
lab var hos_discab "HOS power to dismiss cabinet members"
gen hos_veto=v2exdfvths_ord/4 if v2exdfvths_ord!=.
lab var hos_veto "HOS power to veto legislation"
gen hos_propose=v2exdfpphs_ord
recode hos_propose (0=2) (2=0) //We reorder so that 0 corresponds to less power
replace hos_propose=hos_propose/2 if hos_propose!=.
lab var hos_propose "HOS power to propose legislation"
*HOG forms of power
gen hog_disleg=v2exdjdshg_ord/3 if v2exdjdshg_ord!=.
lab var hog_disleg "HOG power to dissolve legislative assembly"
gen hog_appointcab=v2exdjcbhg_ord/2 if v2exdjcbhg_ord!=.
lab var hog_appointcab "HOG power to appoint cabinet members"
gen hog_discab=v2exdfdshg_ord/3 if v2exdfdshg_ord!=.
lab var hog_discab "HOG power to dismiss cabinet members"
gen hog_veto=v2exdfvthg_ord/4 if v2exdfvthg_ord!=.
lab var hog_veto "HOG power to veto legislation"
gen hog_propose=v2exdfpphg_ord
recode hog_propose (0=2) (2=0) //We reorder so that 0 corresponds to less power
replace hog_propose=hog_propose/2 if hog_propose!=.
lab var hog_propose "HOG power to propose legislation"
*We define a power index for the HOS and HOG as the mean of the 5 variables of power
egen hos_power_linear = rmean(hos_disleg hos_appointcab hos_discab hos_veto hos_propose)
egen hog_power_linear = rmean(hog_disleg hog_appointcab hog_discab hog_veto hog_propose)

// Non-democratically elected leaders
gen hos_nondemoc_year = (v2expathhs==0 | v2expathhs==1 | v2expathhs==2 | v2expathhs==3 | v2expathhs==4 | v2expathhs==5)
gen hog_nondemoc_year = (v2expathhg==0 | v2expathhg==1 | v2expathhg==2 | v2expathhg==3 | v2expathhg==4 | v2expathhg==5)

merge m:1 Country regime using `regime_elec_types', keep(using match) nogen

// We generate the regime mean of several variables
collapse (mean) hos_disleg hos_appointcab hos_discab hos_veto hos_propose hog_disleg hog_appointcab hog_discab hog_veto hog_propose hos_power_linear hog_power_linear v2x_polyarchy v2exhoshog v2ex_elechog v2ex_elechos v2ex_hosconhog v2exaphogp v2exaphos v2ex_legconhog v2ex_legconhos hos_nondemoc_year hog_nondemoc_year elec_pres elec_parl, by(Country regime)

ren v2x_polyarchy polyarchy
ren v2exhoshog hoshog
ren v2ex_elechog elechog
ren v2ex_elechos elechos
ren v2ex_hosconhog hosconhog
ren v2exaphogp exaphogp
ren v2exaphos exaphos
ren v2ex_legconhog legconhog
ren v2ex_legconhos legconhos
ren hos_nondemoc_year hos_nondemoc
ren hog_nondemoc_year hog_nondemoc

lab var regime "Regime"
lab var polyarchy "Electoral democracy index, regime average"
lab var elechog "HOG directly elected, regime average"
lab var elechos "HOS directly elected, regime average"
lab var hosconhog "HOS appoints HOG, regime average"
lab var exaphogp "HOG selection by legislature in practice, regime average"
lab var exaphos "HOS selection by legislature in practice, regime average"
lab var legconhog "HOG appointed by legislature, regime average"
lab var legconhos "HOS appointed by legislature, regime average"
lab var hoshog "HOS is HOG, regime average"
lab var hos_disleg "HOS power to dissolve legislative assembly, regime average"
lab var hos_appointcab "HOS power to appoint cabinet members, regime average"
lab var hos_discab "HOS power to dismiss cabinet members, regime average"
lab var hos_veto "HOS power to veto legislation, regime average"
lab var hos_propose "HOS power to propose legislation, regime average"
lab var hog_disleg "HOG power to dissolve legislative assembly, regime average"
lab var hog_appointcab "HOG power to appoint cabinet members, regime average"
lab var hog_discab "HOG power to dismiss cabinet members, regime average"
lab var hog_veto "HOG power to veto legislation, regime average"
lab var hog_propose "HOG power to propose legislation, regime average"
lab var hos_power_linear "HOS power index, regime average"
lab var hog_power_linear "HOG power index, regime average"
lab var hos_nondemoc "Share of regime years where HOS was nondemocratically appointed"
lab var hog_nondemoc "Share of regime years where HOG was nondemocratically appointed"
lab var elec_pres "Presidential election of our dataset during the regime"
lab var elec_parl "Parliamentary election of our dataset during the regime"

// We check that each regime is only mentionned once
duplicates tag Country regime, gen(temp)
assert temp==0
drop temp

////////////////////////////////////////////////////////////////////////////////
//////// d. Assigning regime characteristics
////////////////////////////////////////////////////////////////////////////////

*HOS is HOG
gen hos_is_hog="Unknown"
replace hos_is_hog="Yes" if hoshog>0.8 & hoshog!=.
replace hos_is_hog="No" if hoshog<0.2 & hoshog!=.
replace hos_is_hog="Ambiguous" if hos_is_hog=="" & hoshog!=.
lab var hos_is_hog "HOS is HOG"

*HOS directly elected
gen hos_direct = "Yes" if elechos>=0.8 & elechos!=.
replace hos_direct = "No" if elechos<=0.2 & elechos!=.
replace hos_direct = "Ambiguous, and elections occurred during the regime" if elechos>0.2 & elechos<0.8 & elec_pres==1 & elechos!=. 
replace hos_direct = "Ambiguous, but elections did not happen during the regime" if elechos>0.2 & elechos<0.8 & elec_pres==0 & elechos!=.
replace hos_direct = "Unknown, and elections occurred during the regime" if hos_direct==""  & elec_pres==1 
replace hos_direct = "Unknown, but elections did not happen during the regime" if hos_direct==""  & elec_pres==0
lab var hos_direct "HOS is directly elected"

*HOG directly elected
gen hog_direct = "Yes" if elechog>=0.8 & elechog!=.
replace hog_direct = "No" if elechog<=0.2 & elechog!=.
replace hog_direct = "Ambiguous, and elections occurred during the regime" if elechog>0.2 & elechog<0.8 & (elec_pres==1 | elec_parl==1) & elechog!=.
replace hog_direct = "Ambiguous, but elections did not happen during the regime" if elechog>0.2 & elechog<0.8 &  elec_pres==0 & elec_parl==0 & elechog!=.
replace hog_direct = "Unknown, and elections occurred during the regime" if hog_direct==""  & (elec_pres==1 | elec_parl==1)
replace hog_direct = "Unknown, but elections did not happen during the regime" if hog_direct==""  & elec_pres==0 & elec_parl==0
replace hog_direct = "Irrelevant: HOS is HOG" if hos_is_hog=="Yes"
lab var hog_direct "HOG is directly elected"

*HOS power index
gen hos_power="Unknown"
replace hos_power = "Very limited" if hos_power_linear<0.25 & hos_power_linear>=0
replace hos_power = "Restrained" if hos_power_linear<0.5 & hos_power_linear>=0.25
replace hos_power = "Strong" if hos_power_linear<0.75 & hos_power_linear>=0.5
replace hos_power = "Very strong" if  hos_power_linear>=0.75 & hos_power_linear!=.
lab var hos_power "HOS power"

*HOG power index
gen hog_power="Unknown"
replace hog_power = "Very limited" if hog_power_linear<0.25 & hog_power_linear>=0
replace hog_power = "Restrained" if hog_power_linear<0.5 & hog_power_linear>=0.25
replace hog_power = "Strong" if hog_power_linear<0.75 & hog_power_linear>=0.5
replace hog_power = "Very strong" if  hog_power_linear>=0.75 & hog_power_linear!=.
replace hog_power = "Irrelevant: HOS is HOG" if hos_is_hog=="Yes"
lab var hog_power "HOG power"

*HOG appointed by HOS
gen hog_appointed_hos = "Yes"  if hosconhog>=0.8 & hosconhog!=.
replace hog_appointed_hos = "No" if hosconhog<=0.2 & hosconhog!=.
replace hog_appointed_hos = "Ambiguous" if hosconhog>0.2 & hosconhog<0.8 & hosconhog!=.
replace hog_appointed_hos = "Unknown" if hog_appointed_hos==""
replace hog_appointed_hos = "Irrelevant: HOS is HOG" if hos_is_hog=="Yes"
lab var hog_appointed_hos "HOG appointed by HOS"

*Parliament chooses HOS
gen hos_parl = "Unknown"
replace hos_parl = "Yes" if legconhos>=0.8 & legconhos!=.
replace hos_parl = "No" if legconhos<=0.2 & legconhos!=.
replace hos_parl = "Ambiguous" if legconhos>0.2 & legconhos<0.8 & legconhos!=.
replace hos_parl = "Ambiguous, and elections occurred during the regime" if hos_parl=="Ambiguous" & elec_parl==1
replace hos_parl = "Ambiguous, but elections did not happen during the regime" if hos_parl=="Ambiguous" & elec_parl==0
lab var hos_parl "HOS is chosen by parliament"

*Parliament chooses HOG
gen hog_parl = "Unknown"
replace hog_parl = "Yes" if legconhog>=0.8 & legconhog!=.
replace hog_parl = "No" if legconhog<=0.2 & legconhog!=.
replace hog_parl = "Ambiguous" if legconhog>0.2 & legconhog<0.8 & legconhog!=.
replace hog_parl = "Ambiguous, and elections occurred during the regime" if hog_parl=="Ambiguous" & elec_parl==1
replace hog_parl = "Ambiguous, but elections did not happen during the regime" if hog_parl=="Ambiguous" & elec_parl==0
lab var hog_parl "HOG is chosen by parliament"

*HOS and HOG nondemocratically elected
ren hos_nondemoc temp_hos_nondemoc
gen hos_nondemoc = 1 if temp_hos_nondemoc>=0.8 & temp_hos_nondemoc!=.
ren hog_nondemoc temp_hog_nondemoc
gen hog_nondemoc = 1 if temp_hog_nondemoc>=0.8 & temp_hog_nondemoc!=.
drop temp_hos_nondemoc temp_hog_nondemoc 

*HOS designation method
gen hos_appointment = "Unknown"
replace hos_appointment = "Directly" if hos_direct=="Yes"
replace hos_appointment = "Parliament" if hos_direct=="No" & hos_parl=="Yes"
replace hos_appointment = "Non-democratic" if hos_nondemoc==1
lab var hos_appointment "HOS designation method"

*HOG designation method
gen hog_appointment = "Unknown"
replace hog_appointment = "Directly" if hog_direct=="Yes"
replace hog_appointment = "Parliament" if hog_direct=="No" & hog_parl=="Yes"
replace hog_appointment = "Appointed by HOS" if hog_direct=="No" & hog_parl=="No" & hog_appointed_hos=="Yes"
replace hog_appointment = "Non-democratic" if hog_nondemoc==1
replace hog_appointment = "Irrelevant: HOS is HOG" if hos_is_hog=="Yes"
lab var hog_appointment "HOG designation method"

*Leaders appointed following a presidential election
gen leaders_pres_elec = "HOS"
replace leaders_pres_elec = "HOG" if hos_appointment!="Directly" & hos_appointment!="Unknown" & hog_appointment=="Directly"
replace leaders_pres_elec = "HOG" if hos_appointment=="Directly" & hog_appointment=="Directly" & hos_power_linear+0.1<hog_power_linear 
replace leaders_pres_elec = "No elections during the regime" if elec_pres==0
lab var leaders_pres_elec "Leaders chosen following presidential elections"

*Leaders appointed following a parliamentary election
gen leaders_parl_elec = "Unknown"
replace leaders_parl_elec = "HOS" if hos_appointment=="Parliament" & hog_appointment!="Parliament" & hog_appointment!="Unknown" & hog_appointment!="Appointed by HOS" 
replace leaders_parl_elec = "HOS" if hos_appointment=="Parliament" & hos_power_linear>hog_power_linear+0.1
replace leaders_parl_elec = "HOG" if hos_appointment!="Parliament" & hos_appointment!="Unknown" & hog_appointment=="Parliament"
replace leaders_parl_elec = "HOG" if hog_appointment=="Parliament" & hos_power_linear+0.1<hog_power_linear
replace leaders_parl_elec = "HOG" if hos_appointment=="Parliament" & hog_appointment=="Appointed by HOS" & hos_power_linear+0.1<hog_power_linear
replace leaders_parl_elec = "HOS" if hos_appointment=="Parliament" & hog_appointment=="Appointed by HOS" & hos_power_linear>hog_power_linear+0.1
replace leaders_parl_elec = "No leader" if hos_appointment!="Parliament" & hog_appointment!="Parliament" & hos_appointment!="Unknown" & hog_appointment!="Unknown"
replace leaders_parl_elec = "No elections during the regime" if elec_parl==0
lab var leaders_parl_elec "Leaders chosen following parliamentary elections"

// Defining regime type

gen reg_type = "Unknown"

*We find the most powerful person in the state
local limit_mixed = 0.1 // Threshold of power difference
gen most_powerful = "Unknown"
replace most_powerful = "HOS" if hos_is_hog=="Yes"
replace most_powerful = "HOS" if hos_power_linear-hog_power_linear > `limit_mixed' & hos_power_linear!=. & hog_power_linear!=. & hos_is_hog!="Yes"
replace most_powerful = "HOG" if hog_power_linear-hos_power_linear > `limit_mixed' & hos_power_linear!=. & hog_power_linear!=. & hos_is_hog!="Yes"
replace most_powerful = "HOS/HOG" if abs(hog_power_linear-hos_power_linear) <= `limit_mixed' & hos_power_linear!=. & hog_power_linear!=. & hos_is_hog!="Yes"

*When the HOS and HOG are the same person
replace reg_type = "Presidential" if hos_appointment=="Directly" & hos_is_hog=="Yes"
replace reg_type = "Parliamentary" if hos_appointment=="Parliament" & hos_is_hog=="Yes"

*If the HOS is most powerful
replace reg_type = "Presidential" if hos_appointment=="Directly" & hos_is_hog!="Yes" & most_powerful=="HOS" 
replace reg_type = "Presidential" if hos_appointment=="Non-democratic" & hos_is_hog!="Yes" & most_powerful=="HOS" 
replace reg_type = "Parliamentary" if hos_appointment=="Parliament" & hos_is_hog!="Yes" & most_powerful=="HOS" 

*If the HOG is most powerful 
replace reg_type = "Presidential" if hog_appointment=="Directly" & hos_is_hog!="Yes" & most_powerful=="HOG"
replace reg_type = "Presidential" if hos_appointment=="Non-democratic" & hos_is_hog!="Yes" & most_powerful=="HOG" 
replace reg_type = "Parliamentary" if hog_appointment=="Parliament" & hos_is_hog!="Yes" & most_powerful=="HOG"

*If the HOG and HOS share equivalent power
replace reg_type = "Presidential" if hog_appointment=="Directly" & hos_appointment=="Directly" & hos_is_hog!="Yes"1 & most_powerful=="HOS/HOG"
replace reg_type = "Presidential" if hog_appointment=="Appointed by HOS" & hos_appointment=="Directly" & hos_is_hog!="Yes" & most_powerful=="HOS/HOG"
replace reg_type = "Parliamentary" if hog_appointment=="Parliament" & hos_appointment=="Parliament" & hos_is_hog!="Yes" & most_powerful=="HOS/HOG"
replace reg_type = "Parliamentary" if hos_appointment=="Parliament" & hog_appointment=="Appointed by HOS" & hos_is_hog!="Yes" & most_powerful=="HOS/HOG"

*If we have not found the regime type, for presidential regimes
gen temp_condition1 = 1 if leaders_parl_elec=="No leader" | (leaders_parl_elec=="HOG" & hog_power=="Very limited")
gen temp_condition2 = 1 if most_powerful=="HOS" & hos_power!="Very limited"  & hos_power!="Restrained"
replace reg_type = "Presidential" if (temp_condition1==1 | temp_condition2==1) & reg_type=="Unknown"
drop temp_condition1 temp_condition2
*If we have not found the regime type, for parliamentary regimes
replace reg_type = "Parliamentary" if most_powerful=="HOG" & hog_appointment=="Unknown" & reg_type=="Unknown"

// We classify some regimes manually
merge 1:1 Country regime using `manual_regime_types', assert(master match) nogen
replace reg_type = manual_regime_type if manual_regime_type!=""
drop manual_regime_type
lab var reg_type "Regime type (presidential, parliamentary or mixed)"

// Importing data for added regimes
tempfile temp
save `temp'
use `manual_regimes', clear
foreach v in leaders_pres_elec leaders_parl_elec reg_type {
	ren `v' manual_`v'
}
merge 1:1 Country regime using `temp', keep(using match)
foreach v in leaders_pres_elec leaders_parl_elec reg_type {
	replace `v' = manual_`v' if _merge==3
}
drop _merge manual_*
replace elec_pres=0 if elec_pres==.
replace elec_parl=0 if elec_parl==.

// Adding regime start and end year
merge 1:1 Country regime using `regime_start_end_years', keep(match) nogen
replace reg_start = reg_start_year if reg_start_year!=.
replace reg_end = reg_end_year if reg_end_year!=.
lab var reg_start "Start year of the regime"
lab var reg_end "End year of the regime"

// Checks 
assert leaders_pres_elec!=""
assert leaders_parl_elec!=""
assert reg_type!=""
assert (leaders_pres_elec=="No elections during the regime") == (elec_pres==0) if Country!="Bosnia-Herz"
assert (leaders_parl_elec=="No elections during the regime") == (elec_parl==0)

////////////////////////////////////////////////////////////////////////////////
//////// e. Saving output
////////////////////////////////////////////////////////////////////////////////

keep Country regime reg_start reg_end reg_type leaders_pres_elec leaders_parl_elec hos_power_linear hog_power_linear
compress
format Country %25s
format regime %30s
format reg_type %15s
format leaders_pres_elec leaders_parl_elec %20s

sort Country reg_start reg_end
notes drop _dta

save "$project_path/data/3_cleaned/regimes", replace
